using System;
using System.Data;
using PCSComUtils.PCSExc;
using System.Linq;
using System.Transactions;
using PCSComUtils.Common;
using PCSComProduct.Items.DS;
using PCSComUtils.MasterSetup.DS;
using PCSComUtils.Common.BO;
using PCSComUtils.DataContext;
using PCSComUtils.DataAccess;
using System.Data.SqlClient;

namespace PCSComProduct.Items.BO
{
    public class ProductItemInfoBO
    {
        private const string THIS = "PCSComProduct.Items.BO.ProductItemInfoBO";

        /// <summary>
        /// Get Cost method from database
        /// </summary>
        /// <returns></returns>
        /// <Author> Tuan TQ, 04 Apr, 2006</Author>
        //[AutoComplete(true)]
        public DataTable GetCostMethod()
        {
            ITM_ProductDS dsProduct = new ITM_ProductDS();
            return dsProduct.GetCostMethod();
        }
        /// <summary>
        /// Check for unique Stock Taking Code
        /// </summary>
        /// <param name="pstrStockTakingCode">Code to check</param>
        /// <returns>true if unique, false if failure</returns>
        //[AutoComplete(true)]
        public bool CheckUniqueStockTakingCode(int pintProductID, string pstrStockTakingCode)
        {
            ITM_ProductDS dsProduct = new ITM_ProductDS();
            return dsProduct.CheckUniqueStockTakingCode(pintProductID, pstrStockTakingCode);
        }

        public ITM_Product GetProductInfo(int iId)
        {
            var db = new PCSDataContext(Utils.Instance.ConnectionString);            
            return db.ITM_Products.Single(e => e.ProductID == iId);
        }
        public ITM_Product GetProductInfo(string strCode)
        {
            var db = new PCSDataContext(Utils.Instance.ConnectionString);
            return db.ITM_Products.Single(e => e.Code == strCode);
        }       
        
        //[AutoComplete(true)]
        public int InsertProduct(ITM_Product product)
        {
            const string METHOD_NAME = "PCSComProduct.Items.BO.ProductItemInfoBO" + ".InsertProduct()";
            using (var trans = new TransactionScope())
            {
                using (var db = new PCSDataContext(Utils.Instance.ConnectionString))
                {
                    try
                    {                        
                        db.ITM_Products.InsertOnSubmit(product);
                        db.SubmitChanges();
                        
                    }
                    catch (SqlException ex)
                    {
                        if (ex.Errors.Count > 1)
                        {
                            if (ex.Number == ErrorCode.SQLDUPLICATE_KEYCODE)
                                throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);
                            throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
                        }
                        throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
                    }
                    trans.Complete();
                }
            }
            return product.ProductID;
        }
        public void UpdateProduct(ITM_Product product)
        {
            const string METHOD_NAME = "PCSComProduct.Items.BO.ProductItemInfoBO.UpdateProduct()";
            using (var trans = new TransactionScope())
            {
                try
                {
                    using (var db = new PCSDataContext(Utils.Instance.ConnectionString))
                    {
                        var obj = db.ITM_Products.SingleOrDefault(e => e.ProductID == product.ProductID);
                        obj.ACAdjustmentMasterID = product.ACAdjustmentMasterID;
                        obj.AGCID = product.AGCID;
                        obj.AutoConversion = product.AutoConversion;
                        obj.AVEG = product.AVEG;
                        obj.BinID = product.BinID;
                        obj.BOMDescription = product.BOMDescription;
                        obj.BomIncrement = product.BomIncrement;
                        obj.BuyerID = product.BuyerID;
                        obj.BuyingUMID = product.BuyingUMID;
                        obj.CategoryID = product.CategoryID;
                        obj.CCNID = product.CCNID;
                        obj.Code = product.Code;
                        obj.ConversionTolerance = product.ConversionTolerance;
                        obj.CostCenterID = product.CostCenterID;
                        obj.CostCenterRateMasterID = product.CostCenterRateMasterID;
                        obj.CostMethod = product.CostMethod;
                        obj.DeleteReasonID = product.DeleteReasonID;
                        obj.DeliveryPolicyID = product.DeliveryPolicyID;
                        obj.Description = product.Description;
                        obj.DesignUMID = product.DesignUMID;
                        obj.ExportTax = product.ExportTax;
                        obj.FinishedGoods = product.FinishedGoods;
                        obj.FormatCodeID = product.FormatCodeID;
                        obj.FreightClassID = product.FreightClassID;
                        obj.Group1 = product.Group1;
                        obj.Group2 = product.Group2;
                        obj.GroupQuantity = product.GroupQuantity;
                        obj.HazardID = product.HazardID;
                        obj.Height = product.Height;
                        obj.HeightUMID = product.HeightUMID;
                        obj.ImportTax = product.ImportTax;
                        obj.InventorID = product.InventorID;
                        obj.IssueSize = product.IssueSize;
                        obj.Length = product.Length;
                        obj.LengthUMID = product.LengthUMID;
                        obj.LicenseFee = product.LicenseFee;
                        obj.ListPrice = product.ListPrice;
                        obj.LocationID = product.LocationID;
                        obj.LotControl = product.LotControl;
                        obj.LotSize = product.LotSize;
                        obj.LTDockToStock = product.LTDockToStock;
                        obj.LTFixedTime = product.LTFixedTime;
                        obj.LTOrderPrepare = product.LTOrderPrepare;
                        obj.LTRequisition = product.LTRequisition;
                        obj.LTSafetyStock = product.LTSafetyStock;
                        obj.LTSalesATP = product.LTSalesATP;
                        obj.LTShippingPrepare = product.LTShippingPrepare;
                        obj.LTVariableTime = product.LTVariableTime;
                        obj.MakeItem = product.MakeItem;
                        obj.MassOrder = product.MassOrder;
                        obj.MasterLocationID = product.MasterLocationID;
                        obj.MaximumStock = product.MaximumStock;
                        obj.MaxProduce = product.MaxProduce;
                        obj.MaxRoundUpToMin = product.MaxRoundUpToMin;
                        obj.MaxRoundUpToMultiple = product.MaxRoundUpToMultiple;
                        obj.MinimumStock = product.MinimumStock;
                        obj.MinProduce = product.MinProduce;
                        obj.OrderPoint = product.OrderPoint;
                        obj.OrderPolicyID = product.OrderPolicyID;
                        obj.OrderQuantity = product.OrderQuantity;
                        obj.OrderQuantityMultiple = product.OrderQuantityMultiple;
                        obj.OrderRuleID = product.OrderRuleID;
                        obj.OtherInfo1 = product.OtherInfo2;
                        obj.ParentProductID = obj.ParentProductID;
                        obj.PartNameVN = product.PartNameVN;
                        obj.PartNumber = product.PartNumber;
                        obj.Picture = product.Picture;
                        obj.PlanType = product.PlanType;
                        obj.PrimaryVendorID = product.PrimaryVendorID;
                        obj.ProductID = product.ProductID;
                        obj.ProductionLineID = product.ProductionLineID;
                        obj.ProductTypeID = product.ProductTypeID;
                        obj.QAStatus = product.QAStatus;
                        obj.QuantitySet = product.QuantitySet;
                        obj.Rate = product.Rate;
                        obj.ReceiveTolerance = product.ReceiveTolerance;
                        obj.RegisteredCode = product.RegisteredCode;
                        obj.Revision = product.Revision;
                        obj.RoutingDescription = product.RoutingDescription;
                        obj.RoutingIncrement = product.RoutingIncrement;
                        obj.SafetyStock = product.SafetyStock;
                        obj.ScrapPercent = product.ScrapPercent;
                        obj.SellingUMID = product.SellingUMID;
                        obj.SetupDate = product.SetupDate;
                        obj.SetUpPair = product.SetUpPair;
                        obj.ShelfLife = product.ShelfLife;
                        obj.ShipToleranceID = product.ShipToleranceID;
                        obj.SourceID = product.SourceID;
                        obj.SpecialTax = product.SpecialTax;
                        obj.Stock = product.Stock;
                        obj.StockTakingCode = product.StockTakingCode;
                        obj.StockUMID = product.StockUMID;
                        obj.TaxCode = product.TaxCode;
                        obj.UpdateDateTime = product.UpdateDateTime;
                        obj.VAT = product.VAT;
                        obj.VendorCurrencyID = product.VendorCurrencyID;
                        obj.VendorLocationID = product.VendorLocationID;
                        obj.VoucherTolerance = product.VoucherTolerance;
                        obj.Weight = product.Weight;
                        obj.WeightUMID = product.WeightUMID;
                        obj.Width = product.Width;
                        obj.WidthUMID = product.WidthUMID;
                        obj.WorkCenterID = product.WorkCenterID;
                        db.SubmitChanges();
                    }
                }
                catch (SqlException ex)
                {
                    if (ex.Errors.Count > 1)
                    {
                        if (ex.Number == ErrorCode.SQLDUPLICATE_KEYCODE)
                            throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);
                        throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
                    }
                    throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
                }
                trans.Complete();
            }
        }

        //[AutoComplete(true)]
        public bool isTwoUnitOfMeasureScalled(int pintUnitID1, int pintUnitID2)
        {
            MST_UMRateDS objMST_UMRateDS = new MST_UMRateDS();
            return objMST_UMRateDS.isTwoUnitOfMeasureScalled(pintUnitID1, pintUnitID2);
        }

        #region IProductItemInfoBO Members

        //[AutoComplete(true)]
        public DataTable GetUnitOfMeasure()
        {
            MST_UnitOfMeasureDS objMST_UnitOfMeasureDS = new MST_UnitOfMeasureDS();

            DataTable dt = objMST_UnitOfMeasureDS.List().Tables[0];
            DataRow drEmptyRow = dt.NewRow();
            dt.Rows.InsertAt(drEmptyRow, 0);
            return dt;
        }

        //[AutoComplete(true)]
        public DataTable GetProductType()
        {
            ITM_ProductDS dsProduct = new ITM_ProductDS();
            DataSet dts = dsProduct.GetProductType();

            if (dts.Tables.Count > 0)
            {
                return dts.Tables[0];
            }
            else
            {
                return null;
            }
        }


        //[AutoComplete(true)]
        public DataTable GetAGC()
        {
            // TODO:  Add ProductItemInfoBO.GetAGC implementation
            const string ID_FIELD = "ID";
            const string VALUE_FIELD = "VALUE";
            MST_AGCDS objMST_AGCDS = new MST_AGCDS();
            DataTable dt = objMST_AGCDS.List().Tables[0];
            DataRow drEmptyRow = dt.NewRow();
            dt.Rows.InsertAt(drEmptyRow, 0);
            return dt;
        }

        //[AutoComplete(true)]
        public DataTable GetQAStatus()
        {
            const string ID_FIELD = "ID";
            const string VALUE_FIELD = "VALUE";

            DataTable dtQAStatus = new DataTable();
            dtQAStatus.Columns.Add(ID_FIELD);
            dtQAStatus.Columns.Add(VALUE_FIELD);
            DataRow drNewRow = dtQAStatus.NewRow();

            drNewRow = dtQAStatus.NewRow();
            dtQAStatus.Rows.Add(drNewRow);

            drNewRow = dtQAStatus.NewRow();
            drNewRow[ID_FIELD] = "1";
            drNewRow[VALUE_FIELD] = "not source quality assured and requires inspection";
            dtQAStatus.Rows.Add(drNewRow);

            drNewRow = dtQAStatus.NewRow();
            drNewRow[ID_FIELD] = "2";
            drNewRow[VALUE_FIELD] = "not source quality assured but does not require inspection";
            dtQAStatus.Rows.Add(drNewRow);

            drNewRow = dtQAStatus.NewRow();

            drNewRow[ID_FIELD] = "3";
            drNewRow[VALUE_FIELD] = "source quality assured";
            dtQAStatus.Rows.Add(drNewRow);

            return dtQAStatus;
        }

        //[AutoComplete(true)]
        public DataTable GetCategory()
        {
            ITM_CategoryDS objITM_CategoryDS = new ITM_CategoryDS();
            DataTable dt = objITM_CategoryDS.ListForProduct().Tables[0];
            DataRow drEmptyRow = dt.NewRow();
            dt.Rows.InsertAt(drEmptyRow, 0);
            return dt;
        }

        //[AutoComplete(true)]
        public DataTable GetSource()
        {
            ITM_SourceDS objITM_SourceDS = new ITM_SourceDS();
            DataTable dt = objITM_SourceDS.List().Tables[0];
            DataRow drEmptyRow = dt.NewRow();
            dt.Rows.InsertAt(drEmptyRow, 0);

            return dt;
        }

        //[AutoComplete(true)]
        public DataTable GetHarzard()
        {
            ITM_HazardDS objITM_HazardDS = new ITM_HazardDS();
            DataTable dt = objITM_HazardDS.List().Tables[0];
            DataRow drEmptyRow = dt.NewRow();
            dt.Rows.InsertAt(drEmptyRow, 0);

            return dt;
        }

        //[AutoComplete(true)]
        public DataTable GetFreightClass()
        {
            ITM_FreightClassDS objITM_FreightClassDS = new ITM_FreightClassDS();
            DataTable dt = objITM_FreightClassDS.List().Tables[0];
            DataRow drEmptyRow = dt.NewRow();
            dt.Rows.InsertAt(drEmptyRow, 0);

            return dt;
        }

        //[AutoComplete(true)]
        public DataTable GetDeleteReason()
        {
            ITM_DeleteReasonDS objITM_DeleteReasonDS = new ITM_DeleteReasonDS();
            DataTable dt = objITM_DeleteReasonDS.List().Tables[0];
            DataRow drEmptyRow = dt.NewRow();
            dt.Rows.InsertAt(drEmptyRow, 0);

            return dt;
        }

        //[AutoComplete(true)]
        public DataTable GetFormatCodes()
        {
            ITM_FormatCodeDS objITM_FormatCodeDS = new ITM_FormatCodeDS();

            DataTable dt = objITM_FormatCodeDS.List().Tables[0];
            DataRow drEmptyRow = dt.NewRow();
            dt.Rows.InsertAt(drEmptyRow, 0);

            return dt;
        }


        //[AutoComplete(true)]
        public DataTable GetCCN()
        {
            UtilsBO objUtilsBO = new UtilsBO();
            return objUtilsBO.ListCCN().Tables[0];
        }

        //[AutoComplete(true)]
        public DataTable GetDeliveryPolicy()
        {
            ITM_DeliveryPolicyDS objITM_DeliveryPolicyDS = new ITM_DeliveryPolicyDS();

            DataTable dt = objITM_DeliveryPolicyDS.List().Tables[0];
            DataRow drEmptyRow = dt.NewRow();
            dt.Rows.InsertAt(drEmptyRow, 0);

            return dt;
        }

        //[AutoComplete(true)]
        public DataTable GetOrderPolicy()
        {
            ITM_OrderPolicyDS objITM_OrderPolicyDS = new ITM_OrderPolicyDS();

            DataTable dt = objITM_OrderPolicyDS.List().Tables[0];
            DataRow drEmptyRow = dt.NewRow();
            dt.Rows.InsertAt(drEmptyRow, 0);
            return dt;
        }

        //[AutoComplete(true)]
        public DataTable GetShipTolerence()
        {
            ITM_ShipToleranceDS objITM_ShipToleranceDS = new ITM_ShipToleranceDS();

            DataTable dt = objITM_ShipToleranceDS.List().Tables[0];
            DataRow drEmptyRow = dt.NewRow();
            dt.Rows.InsertAt(drEmptyRow, 0);
            return dt;
        }

        //[AutoComplete(true)]
        public DataTable GetBuyer()
        {
            ITM_BuyerDS objITM_BuyerDS = new ITM_BuyerDS();

            DataTable dt = objITM_BuyerDS.List().Tables[0];
            DataRow drEmptyRow = dt.NewRow();
            dt.Rows.InsertAt(drEmptyRow, 0);
            return dt;
        }

        //[AutoComplete(true)]
        public DataTable GetVendorLocation()
        {
            MST_PartyLocationDS objMST_PartyLocationDS = new MST_PartyLocationDS();

            DataTable dt = objMST_PartyLocationDS.ListForCombo().Tables[0];
            DataRow drEmptyRow = dt.NewRow();
            dt.Rows.InsertAt(drEmptyRow, 0);
            return dt;
        }


        //[AutoComplete(true)]
        public DataTable GetOrderRule()
        {
            ITM_OrderRuleDS objITM_OrderRuleDS = new ITM_OrderRuleDS();

            DataTable dt = objITM_OrderRuleDS.List().Tables[0];
            DataRow drEmptyRow = dt.NewRow();
            dt.Rows.InsertAt(drEmptyRow, 0);
            return dt;
        }

        //[AutoComplete(true)]
        public DataSet GetLocation()
        {
            DataSet dstLocation = new DataSet();
            DataTable dt;
            DataRow drEmptyRow;
            //get Master Location
            MST_MasterLocationDS objMST_MasterLocationDS = new MST_MasterLocationDS();
            dt = objMST_MasterLocationDS.List().Tables[0].Copy();
            drEmptyRow = dt.NewRow();
            dt.Rows.InsertAt(drEmptyRow, 0);
            dstLocation.Tables.Add(dt);

            //Get Location
            MST_LocationDS objMST_LocationDS = new MST_LocationDS();
            dt = objMST_LocationDS.List().Tables[0].Copy();
            drEmptyRow = dt.NewRow();
            dt.Rows.InsertAt(drEmptyRow, 0);
            dstLocation.Tables.Add(dt);

            //Get Bin
            MST_BINDS objMST_BINDS = new MST_BINDS();
            dt = objMST_BINDS.List().Tables[0].Copy();
            drEmptyRow = dt.NewRow();
            dt.Rows.InsertAt(drEmptyRow, 0);
            dstLocation.Tables.Add(dt);

            return dstLocation;
        }

        //[AutoComplete(true)]
        public bool ValidateBusiness(object pObjectDetail)
        {
            const string METHOD_NAME = THIS + ".ValidateBusiness()";
            ITM_Product objITM_ProductVO = (ITM_Product)pObjectDetail;
            if (objITM_ProductVO.CategoryID > 0)
            {
                //Check this category.
                //This category must be a leaf node 
                ITM_CategoryDS objITM_CategoryDS = new ITM_CategoryDS();
                int iCategoryID = objITM_ProductVO.CategoryID ?? 0;
                if (!objITM_CategoryDS.IsLeafNode(iCategoryID))
                {
                    throw new PCSBOException(ErrorCode.MSG_PRODUCTINFO_CATEGORY_NOTALEAFNODE, METHOD_NAME, null);
                }
            }
            return true;
        }
        //[AutoComplete(true)]
        public void DeleteProduct(int pintProductID)
        {
            ITM_ProductDS objITM_ProductDS = new ITM_ProductDS();
            objITM_ProductDS.Delete(pintProductID);
        }
        public void Delete(int pintProductID)
        {
            const string METHOD_NAME = THIS + ".Delete()";
            try
            {
                using (var trans = new TransactionScope())
                {
                    using (var db = new PCSDataContext(Utils.Instance.ConnectionString))
                    {
                        int iCheck=0;
                        var listRouting = from objdetail in db.ITM_Routings
                                         where objdetail.ProductID == pintProductID
                                         select objdetail;
                        var listBom = from objdetail in db.ITM_BOMs
                                          where objdetail.ProductID == pintProductID
                                          select objdetail;
                        if (listRouting != null)
                        {
                            try
                            {
                                iCheck = listRouting.ToArray().Count();
                            }
                            catch
                            { 
                            }
                        }
                        if (iCheck > 0)
                        {
                            throw new PCSDBException(ErrorCode.SQLCASCADE_PREVENT_KEYCODE, METHOD_NAME, new PCSDBException());
                        }
                        if (listBom != null)
                        {
                            try
                            {
                                iCheck = listBom.ToArray().Count();
                            }
                            catch
                            {
                            }
                        }
                        if (iCheck > 0)
                        {
                            throw new PCSDBException(ErrorCode.SQLCASCADE_PREVENT_KEYCODE, METHOD_NAME, new PCSDBException());
                        }
                        var obj = db.ITM_Products.SingleOrDefault(e => e.ProductID == pintProductID);
                        db.ITM_Products.DeleteOnSubmit(obj);
                        db.SubmitChanges();
                        trans.Complete();

                    }

                }
            }
            catch (SqlException ex)
            {
                if (ex.Number == ErrorCode.SQLCASCADE_PREVENT_KEYCODE)
                {
                    throw new PCSDBException(ErrorCode.CASCADE_DELETE_PREVENT, METHOD_NAME, ex);
                }
                if (ex.Errors.Count > 1)
                {
                    if (ex.Number == ErrorCode.SQLDUPLICATE_KEYCODE)
                        throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);
                    throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
                }
                throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
            }
            catch (PCSDBException ex)
            {
                if (ex.mCode == ErrorCode.SQLCASCADE_PREVENT_KEYCODE)
                {
                    throw new PCSDBException(ErrorCode.CASCADE_DELETE_PREVENT, METHOD_NAME, ex);
                }
                if (ex.mCode == ErrorCode.MESSAGE_NOT_ENOUGH_COMPONENT_TO_COMPLETE)
                    throw new PCSDBException(ErrorCode.MESSAGE_NOT_ENOUGH_COMPONENT_TO_COMPLETE, METHOD_NAME, ex);
                throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
            }
        }

        //[AutoComplete(true)]
        public string GetVendorCodeAndName(int pintVendorID)
        {
            MST_PartyDS objMST_PartyDS = new MST_PartyDS();
            return objMST_PartyDS.GetPartyCodeAndName(pintVendorID);
        }

        //[AutoComplete(true)]
        public string GetCurrencyCode(int pintCurencyID)
        {
            MST_CurrencyVO voCurrency = (MST_CurrencyVO)(new MST_CurrencyDS()).GetObjectVO(pintCurencyID);
            if (voCurrency != null)
            {
                return voCurrency.Code;
            }

            return string.Empty;
        }

        //[AutoComplete(true)]
        public bool HasVendorDeliverySchedule(int pintID)
        {
            ITM_ProductDS dsProduct = new ITM_ProductDS();
            return dsProduct.HasVendorDeliverySchedule(pintID);
        }

        #endregion
        #region HACKED: Thachnn: 01 / 03 / 2006 : add DB function to create data for ItemInformationReport


        /// <summary>
        /// Get report data for specific Product.
        /// When execute the SQL string, data adapter will return 5 table, Meta, Stock,BOm, Routing, StandardCost.
        /// We will name these table with provided names (get from Parameters).
        /// <author>Thachhnn: 01 03 2006 </author>
        /// </summary>
        /// <param name="pnProductID"Which Product to get info></param>
        /// <param name="pstrMetaDataTableName"></param>
        /// <param name="pstrStockStatusTableName"></param>
        /// <param name="pstrBOMTableName"></param>
        /// <param name="pstrStandardCostTableName"></param>
        /// <returns>DataSet with multiple datatable. This dataset will contain MetaDataTable, StockStatus, BOM, Routing, StandardCost</returns>		
        //[AutoComplete(true)]
        public DataSet GetItemInformationData(int pnProductID,
            string pstrMetaDataTableName, string pstrStockStatusTableName, string pstrBOMTableName, string pstrRoutingTableName, string pstrStandardCostTableName)
        {
            ITM_ProductDS objITM_ProductDS = new ITM_ProductDS();
            return objITM_ProductDS.GetItemInformationData(pnProductID,
                pstrMetaDataTableName, pstrStockStatusTableName, pstrBOMTableName, pstrRoutingTableName, pstrStandardCostTableName);
        }

        //Khanhpq
        public DataSet GetListBomByproductId(int pintProductID)
        {
            ITM_ProductDS objDS = new ITM_ProductDS();
            return objDS.GetListBomByproductId(pintProductID);
        }
        public DataSet GetListRountingByproductId(int pintProductID)
        {
            ITM_ProductDS objDS = new ITM_ProductDS();
            return objDS.GetListRountingByproductId(pintProductID);
        }
        //Copy Bom
        public int CopyBom(ITM_BOM objVO )
        {
            using (var trans = new TransactionScope())
            {
                using (var db = new PCSDataContext(Utils.Instance.ConnectionString))
                {
                    try
                    {
                        db.ITM_BOMs.InsertOnSubmit(objVO);
                        db.SubmitChanges();

                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    trans.Complete();
                }
            }
            return objVO.BomID;
        }

        public int CopyRounting(ITM_Routing objVO)
        {
            using (var trans = new TransactionScope())
            {
                using (var db = new PCSDataContext(Utils.Instance.ConnectionString))
                {
                    try
                    {
                        db.ITM_Routings.InsertOnSubmit(objVO);
                        db.SubmitChanges();

                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    trans.Complete();
                }
            }
            return objVO.RoutingID;
        }
        
        #endregion ENDHACKED: Thachnn: 01 / 03 / 2006 : add DB function to create data for ItemInformationReport
    }
}
